//
//  MyBaseHandle.m
//  旅游咨询
//
//  Created by 高建 on 15/4/21.
//  Copyright (c) 2015年 xubin. All rights reserved.
//

#import "MyBaseHandle.h"

@implementation MyBaseHandle

+(instancetype)shareDataBaseHandle
{
    static MyBaseHandle *dataBaseHandle = nil;
    if (dataBaseHandle == nil) {
        dataBaseHandle = [[MyBaseHandle alloc] init];
        
    }
    return dataBaseHandle;
}
static sqlite3 *db = nil;
-(void)openDB
{
    if (db != nil) {
        NSLog(@"数据库打开了");
        return;
    }
    //创建本地文件路径
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [array lastObject];
    NSString *sqliteFilePath = [documentPath stringByAppendingPathComponent:@"InLand.sqlite"];
    NSLog(@"sqlite = %@", sqliteFilePath);
    
    //打开数据库
    BOOL result = sqlite3_open(sqliteFilePath.UTF8String, &db);
    if (result == SQLITE_OK) {
        NSLog(@"数据库打开成功");
    }else{
        NSLog(@"数据库打开失败");
    }
    
    
}
-(void)createDB
{
    NSString *createSql = @"CREATE TABLE IF NOT EXISTS InLand(name_zh TEXT, cover_s TEXT, cover_route_map_cover TEXT)";
    //执行语句创建表
    BOOL result = sqlite3_exec(db, createSql.UTF8String, NULL, NULL, nil);
    if (result == SQLITE_OK) {
        NSLog(@"创建表成功");
    }else{
        NSLog(@"创建表失败");
    }
}

//插入数据
-(void)insertInLandCity:(InLandCityModel *)InLand
{
    NSString *inserSql = [NSString stringWithFormat:@"INSERT INTO InLand(name_zh , cover_s , cover_route_map_cover ) VALUES('%@','%@','%@')",InLand.name_zh, InLand.cover_s, InLand.cover_route_map_cover];
    int result = sqlite3_exec(db,inserSql.UTF8String, NULL, NULL, nil);
    if (result == SQLITE_OK) {
        NSLog(@"插入成功");
    }else{
        NSLog(@"插入失败");
    }
}

//删除表

//查询数据
-(NSMutableArray *)selectInLand
{
    NSString *selectSql = @"SELECT * FROM InLand";
    sqlite3_stmt *stmt = nil;
    BOOL result = sqlite3_prepare_v2(db, selectSql.UTF8String, -1, &stmt, nil);
    if (result == SQLITE_OK) {
        NSLog(@"查询准备成功");
        NSMutableArray *array = [NSMutableArray array];
        //开始循环查询每一行数据
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            
            
            const unsigned char *name_zh = sqlite3_column_text(stmt, 0);
            const unsigned char *cover_s = sqlite3_column_text(stmt, 1);
            const unsigned char *cover_route_map_cover = sqlite3_column_text(stmt, 2);
           
            
            
            NSString *name_zhStr = [NSString stringWithUTF8String:(const char *)name_zh];
            NSString *cover_sStr = [NSString stringWithUTF8String:(const char *)cover_s];
            NSString *cover_route_map_coverStr = [NSString stringWithUTF8String:(const char *)cover_route_map_cover];
           
            
            InLandCityModel *myLand = [[InLandCityModel alloc]init];
            myLand.name_zh = name_zhStr;
            myLand.cover_s = cover_sStr;
            myLand.cover_route_map_cover = cover_route_map_coverStr;
            
            [array addObject:myLand];
            [myLand release];
            
            
        }
        return array;
        
    }else{
        NSLog(@"查询失败");
    }
    return nil;

}
-(void)deleteInLandCity
{
    NSString *deleteSql = [NSString stringWithFormat:@"DELETE FROM InLand"];
    int result = sqlite3_exec(db, deleteSql.UTF8String, NULL, NULL, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }else{
        NSLog(@"删除失败");
    }
}

@end
